<?php
/**
 * Description: 错题上报
 * User: devforma
 * Date: 15/5/4
 * Time: 17:43
 */

class ReportErrorQuestion implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        if (empty($queryArgs['question_id']) || empty($queryArgs['error_type']))
            return genOutput(1000);

        $existId = $cmsdb->getVar('SELECT `id` FROM `data_error_question` WHERE `question_id`=:qid',
            array(':qid' => $queryArgs['question_id']));

        $date = date('Y-m-d H:i:s');

        switch ($queryArgs['error_type']) {
            case '1': //图文有错
                if ($existId) {
                    $cmsdb->query("UPDATE `data_error_question` SET `content_error_count`=`content_error_count`+1,
                    `last_modified`='{$date}' WHERE `id`={$existId}");
                } else {
                    $cmsdb->insert('data_error_question', array(
                        'question_id' => $queryArgs['question_id'],
                        'last_modified' => $date,
                        'content_error_count' => 1,
                        'answer_error_count' => 0,
                        'resolved' => 0
                    ));
                }

                if (!empty(trim($queryArgs['error_reason']))) {
                    $cmsdb->insert('data_ugc_question_analysis', array(
                        'user_id' => $queryArgs['user_id'],
                        'question_id' => $queryArgs['question_id'],
                        'analysis' => $queryArgs['error_reason'],
                        'type' => 1
                    ));
                }

                break;

            case '2': //答案有错
            case '3': //解析有错
                if ($existId) {
                    $cmsdb->query("UPDATE `data_error_question` SET `answer_error_count`=`answer_error_count`+1,
                    `last_modified`='{$date}' WHERE `id`={$existId}");
                } else {
                    $cmsdb->insert('data_error_question', array(
                        'question_id' => $queryArgs['question_id'],
                        'last_modified' => $date,
                        'content_error_count' => 0,
                        'answer_error_count' => 1,
                        'resolved' => 0
                    ));
                }

                if (!empty(trim($queryArgs['error_reason']))) {
                    $cmsdb->insert('data_ugc_question_analysis', array(
                        'user_id' => $queryArgs['user_id'],
                        'question_id' => $queryArgs['question_id'],
                        'analysis' => $queryArgs['error_reason'],
                        'type' => 1
                    ));
                }

                break;

            case '4': //更好的解析
                if (!empty($queryArgs['my_analysis'])) {
                    $cmsdb->insert('data_ugc_question_analysis', array(
                        'user_id' => $queryArgs['user_id'],
                        'question_id' => $queryArgs['question_id'],
                        'analysis' => $queryArgs['my_analysis'],
                        'type' => 0
                    ));
                }

                break;

            default:
        }

        return genOutput(1);
    }

}